import { BaseController } from '../BaseController';
import {Md5} from 'ts-md5/dist/md5';

// 参考文档： http://open.4399.cn/document/doc/doc/id/20
export class m4399 extends BaseController {
    protected SECRET: string = "";

    async Start(){
        let query = this.request.query;
        
        let orderid = query.orderid;
        let p_type = query.p_type;
        let uid = query.uid;
        let money = query.money;
        let gamemoney = query.gamemoney;
        let time = query.time;
        let sign = query.sign;
        
        let serverid = "";
        if (query.serverid != null){
            serverid = query.serverid;
        }
        let mark = "";
        if (query.mark != null){
            mark = query.mark;
        }
        let extra = "";
        if (query.extra != null){
            extra = query.extra;
        }
        
        // $sign = md5($orderid.$uid.$money.$gamemoney.$serverid.$secret.$mark.$time.$extra); 
        let data = orderid + p_type + uid + money + gamemoney + serverid + this.SECRET + mark + time + extra;
        let strMd5 = <string>Md5.hashStr(data);

        let verify = 0;
        if (strMd5 == sign){
            // 验证成功
            verify = 1;
        }
        
        let ret = await this.WriteOrderInfoToDb(1, "4399渠道", orderid, p_type, 
            uid, money, gamemoney, time, extra, verify);

        let code: any = null;
        let json = {
            status: 2,
            code: code,
            money: money + "",
            gamemoney: gamemoney + "",
            msg: "充值成功: " + ret
        };

        this.response.send(JSON.stringify(json));
    }
}
